using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls
{
    public class PdfContentControlEdit : BaseContentControlEdit
    {
        private FileUpload fileUpload;
        private HyperLink editLink;
        private TextBox editName;
        private Label editNoFile;

        private HyperLink displayLink;
        private Label displayNoFile;

        protected override void CreateDisplayControls(PlaceHolder placholder)
        {
            displayLink = new HyperLink();
            displayLink.ID = "displayLink";
            placholder.Controls.Add(displayLink);

            displayNoFile = new Label();
            displayNoFile.Text = "No file uploaded";
            placholder.Controls.Add(displayNoFile);
        }

        protected override void CreateEditControls(PlaceHolder placholder)
        {
            fileUpload = new FileUpload();
            fileUpload.ID = "fileUpload";
            placholder.Controls.Add(fileUpload);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            placholder.Controls.Add(new LiteralControl("Name:"));

            editName = new TextBox();
            editName.ID = "editName";
            placholder.Controls.Add(editName);

            placholder.Controls.Add(new LiteralControl("<br/>"));

            editLink = new HyperLink();
            editLink.ID = "editLink";
            placholder.Controls.Add(editLink);

            editNoFile = new Label();
            editNoFile.Text = "No file uploaded";
            placholder.Controls.Add(editNoFile);
        }

        private PdfContentControlProperties properties;
        protected PdfContentControlProperties Properties
        {
            get 
            {
                if (properties == null)
                {
                    properties = new PdfContentControlProperties();
                }
                return properties;
            }
        }

        protected override BaseContentControlProperties GetProperties()
        {
            return Properties;
        }

        protected override void SetProperties(BaseContentControlProperties properties)
        {
            this.properties = properties as PdfContentControlProperties;
        }

        protected override void SaveToProperties()
        {
            if (fileUpload.HasFile)
            {
                Acms.Bll.File file = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Pdf, fileUpload.PostedFile, fileUpload.FileBytes);
                file.Update();
                Properties.ResourceId = file.Id;
            }
            Properties.Name = editName.Text;
        }

        protected override void DeleteFromProperties()
        {
            if (Properties != null && Properties.ResourceId > 0)
            {
                Acms.Bll.File file = Acms.Bll.File.GetById(Properties.ResourceId);
                if (file != null)
                {
                    file.Delete();
                }
            }
        }

        protected override void DataBindChildren()
        {
            displayLink.ToolTip = displayLink.Text = editLink.ToolTip = editLink.Text = Properties.Name;
            displayLink.Target = editLink.Target = "_blank";
            displayLink.NavigateUrl = editLink.NavigateUrl = string.Concat("~/File.aspx?Id=", Properties.ResourceId.ToString());

            editName.Text = Properties.Name;

            displayNoFile.Visible = editNoFile.Visible = !Properties.HasResource();
            displayLink.Visible = editLink.Visible = Properties.HasResource();

            base.DataBindChildren();
        }
    }
}
